;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Account.MoneyBackPlan", [])
        .controller("MoneyBackPlanCtrl", ["$scope", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "$filter", function ($scope, ybWidget, ybHttp, MESG_TYPE, $timeout, $filter) {
            $scope.activeFlag = [true, false, false];
            $scope.markInfo = {
                allCheckedOrder: false,
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.tableConfig = {
                scrollWidth: 940,
                tableCol: [
                    {
                        name: "客户名称",
                        dataField: "clientName",
                        width: 300,
                        isShow: true
                    },
                    {
                        name: "单据编号",
                        dataField: "billNo",
                        width: 300,
                        isShow: true,
                    },
                    {
                        name: "应收款金额",
                        dataField: "shouldReceivePrice",
                        width: 200,
                        isShow: true,
                        type: "click",
                        clickFn: "viewReceiverDetail"
                    },
                    {
                        name: "已回款金额",
                        dataField: "receivePrice",
                        width: 200,
                        isShow: true,
                    },
                    {
                        name: "创建人",
                        dataField: "createName",
                        width: 200,
                        isShow: true,
                    }
                ]
            };

            $scope.exportParam = {
                allUrl: ybHttp.RequestUrl("device", "/payment/exportPaymentReport")
            };

            $scope.dataList = [];
            $scope.toolbarConfig = {};
            $scope.condition = {};

            function initData() {
                $scope.markInfo.allCheckedOrder = false;
                $scope.detailPage.condition = {};
                $scope.detailPage.dataList = [];
            }

            $scope.changeTab = function (index) {
                angular.forEach($scope.activeFlag, function (activeFlag) {
                    activeFlag = false;
                });
                $scope.activeFlag[index] = true;
                switch (index) {
                    case 0:
                        initData();
                        getDataList();
                        break;
                    default:
                        break;
                }
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                    return;
                }
                $timeout(function () {
                    $scope.toolbarConfig["show" + toolbar] = true;
                });
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            function checkAll(list) {
                var selectAll = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.selectAll = function (list, type) {
                angular.forEach(list, function (item) {
                    item.isChecked = $scope.markInfo["allChecked" + type];
                });
            };

            $scope.selectSingle = function (item, list, type) {
                if (!item.isChecked) {
                    $scope.markInfo["allChecked" + type] = false;
                } else {
                    if (!checkAll(list)) {
                        $scope.markInfo["allChecked" + type] = false;
                    } else {
                        $scope.markInfo["allChecked" + type] = true;
                    }
                }
            };

            $scope.tdClick = function (item, fnType) {
                switch (fnType) {
                    case "viewReceiverDetail":
                        viewReceiverDetail(item);
                        break;
                    default:
                        break;
                }
            };

            function getDataList(curPage) {
                $scope.markInfo.curRow = -1;

                var service = "device";
                var reqUrl = "/payment/list";

                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                var param = {
                    pageSize: $scope.pageInfo.pageSize,
                    pageNum: $scope.pageInfo.curPage,
                    filters: angular.copy($scope.condition)
                };

                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.importData = function () {
                var param = {
                    name: "回款计划",
                    templateReqService: "device",
                    templateReqUrl: "/payment/downloadExcel",
                    importReqService: "device",
                    importReqUrl: "/payment/importExcel",
                    importErrorUrl: ""
                };
                ybWidget.ImportFile(param).then(function (data) {
                    getDataList(1);
                });
            };



            function viewReceiverDetail(item) {
                $scope.detailPage.condition.clientNo = item.clientNo;
                $scope.detailPage.condition.billNo = item.billNo;
                getDetailDataList(1).then(() => {
                    $scope.changeTab(1);
                })
            }

            $scope.detailPage = {};
            $scope.detailPage.markInfo = {
                curRow: -1
            };
            $scope.detailPage.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.detailPage.condition = {};
            $scope.detailPage.dataList = [];
            $scope.detailPage.tableConfig = {
                scrollWidth: 320,
                tableCol: [
                    {
                        name: "计划回款日期",
                        dataField: "planPaymentDateShow",
                        width: 160,
                        isShow: true
                    },
                    {
                        name: "应回款金额",
                        dataField: "planReceivePrice",
                        width: 160,
                        isShow: true
                    },
                ]
            };

            $scope.detailPage.search = function (curpage) {
                getDetailDataList(curpage);
            };

            $scope.detailPage.selectRow = function (index) {
                $scope.detailPage.markInfo.curRow = index
            };

            function getDetailDataList(curPage) {
                $scope.detailPage.markInfo.curRow = -1;

                var service = "device";
                var reqUrl = "/payment/listDetail";

                if (!!curPage) {
                    $scope.detailPage.pageInfo.curPage = curPage;
                }
                var param = {
                    pageSize: $scope.detailPage.pageInfo.pageSize,
                    pageNum: $scope.detailPage.pageInfo.curPage,
                    filters: angular.copy($scope.detailPage.condition)
                };

                return ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.planPaymentDateShow = $filter("YbDate")(item.planPaymentDate, 'yyyy-MM-dd');
                    });
                    $scope.detailPage.pageInfo.totalRows = resp.value.total;
                    $scope.detailPage.pageInfo.totalPages = resp.value.pages;
                    $scope.detailPage.dataList = resp.value.list;
                });
            }


            (function () {
                getDataList(1);
            })();
        }])
    ;
})(window, window.angular);
